<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/7/12
 * Time: 18:47
 */
namespace app\admin\controller;

use app\admin\BaseController;
use think\Cookie;

class News extends BaseController
{
    public function index()
    {
        $data = input('param.');
        $query = http_build_query($data);   //将搜索条件转化为 catid=1&create_time=xx&end_time=xx&title=xxxx
        //halt($query);

        $whereData = [];
        //转换前端传进来的查询条件
        if(!empty($data['start_time']) && !empty($data['end_time']) && $data['start_time']<$data['end_time'])
        {
            $whereData['create_time'] = [
                ['gt',strtotime($data['start_time'])],    //大于
                ['lt',strtotime($data['end_time'])]       //小于
            ];
        }
        if(!empty($data['catid']))
        {
            $whereData['catid'] = intval($data['catid']);
        }
        if(!empty($data['title']))
        {
            $whereData['title'] = ['like','%'.$data['title'].'%'];
        }


        //模式一的分页
        //$news = model('News')->getNews();

        //模式二:自己从原理去封装一个分页,并结合第三方laypage插件来进行分页
        // page size start  limit start size
        $this->getPageAndSize($data);
        //$whereData['page'] = $this->page;
        //$whereData['size'] = $this->size;

        //$whereData['page'] = !empty($data['page'])?$data['page']:1;
        //$whereData['size'] = !empty($data['size'])?$data['size']:config('paginate.list_rows');

        //获取表里的数据
        $news = model('News')->getNewsByCondition($whereData,$this->start,$this->size);

        //获取满足条件的数据总数 从而转化为可以显示多少页
        $total = model('News')->getNewsCountByCondition($whereData);

        //计算出有多少页
        //$pageTotal = ceil($total/$whereData['size']);   // ceil小技巧,1.1向上取取整变为2,显示两页
        $pageTotal = ceil($total/$this->size);

        return $this->fetch('',[
            'cats'=>config('cat.lists'),
            'news'=>$news,
            'pageTotal'=>$pageTotal,
            //'curr'=>$whereData['page'],
            'curr'=>$this->page,
            'start_time'=>empty($data['start_time'])?'':$data['start_time'],
            'end_time'=>empty($data['end_time'])?'':$data['end_time'],
            'catid'=>empty($data['catid'])?'':$data['catid'],
            'title'=>empty($data['title'])?'':$data['title'],
            'query'=>$query
        ]);
    }

    public function add()
    {
        if(request()->isPost())
        {
            //return $this->result('',0,'新增失败');    测试新增失败用的
            $data = input('post.');

            //验证器验证数据

            //入库操作
            try{
                $id = model('news')->add($data);
            }catch (\Exception $e)
            {
                //exception($e->getMessage());
                return $this->result('',0,'新增失败');
            }
            if($id)
            {
                return $this->result(['jump_url'=>url('news/index')],1,'ok');
            }else{
                return $this->result('',0,'新增失败');
            }
        }else{
            return $this->fetch('',[
                'cats'=>config('cat.lists')
            ]);
        }

    }

}